import requestMeeting, { request } from '@/plugins/axios'
import { API_PROTAL_PREFIX } from '@/utils/constant'
import {
	ExpandTreeParams,
	SearchDeptResponse,
	SearchTemplateExactResponse,
	SearchTemplateResponse,
	UserInDeptEntity,
} from './interface'

export const fetchUserExact = async (searchKey: string) =>
	requestMeeting<UserInDeptEntity>({
		url: `/organizations/users/exact`,
		method: 'get',
		baseURL: API_PROTAL_PREFIX,
		params: {
			key: searchKey,
			scope: 1,
		},
	}).then((res) => (res.success && res.data ? res.data : undefined))

export const fetchTemplates = async (searchKey: string) =>
	request<SearchTemplateExactResponse>({
		url: `/personal_templates`,
		method: 'get',
		params: {
			e164: searchKey,
		},
	}).then((res) => (res.success && res.personal_templates ? res.personal_templates : undefined))

export const fetchPendingInit = async () =>
	requestMeeting<SearchDeptResponse>({
		url: '/organizations/tree',
		method: 'get',
		baseURL: API_PROTAL_PREFIX,
		params: {
			scope: 1,
		},
	}).then((res) => (res.success && res.data ? res.data : undefined))

export const fetchUserLike = async (fromSearchVal: string, limit: number) =>
	requestMeeting<SearchDeptResponse>({
		url: '/organizations/users/like',
		method: 'get',
		baseURL: API_PROTAL_PREFIX,
		params: {
			scope: 1,
			key: fromSearchVal,
			limit,
		},
	}).then((res) => (res.success && res.data ? res.data : undefined))

export const fetchTemplatesSearch = async (fromSearchVal: string) =>
	request<SearchTemplateResponse>({
		url: '/personal_templates/search',
		method: 'get',
		params: {
			key: fromSearchVal,
		},
	}).then((res) => (res.success && res ? res : undefined))

export const fetchUser = async (params: Partial<ExpandTreeParams>) =>
	requestMeeting<{ users: UserInDeptEntity[] }>({
		url: '/organizations/user',
		method: 'get',
		baseURL: API_PROTAL_PREFIX,
		params,
	}).then((res) => (res.success && res.data ? res.data : undefined))

export const fetchSimpleCascadeTypeAndCheckAddTemplateMap = async (data: {
	cur_template_e164: string
	cascade_e164s: string
}) =>
	requestMeeting<{
		cascade_type: { [key: string]: 0 | 1 }
		template_can_add: { [key: string]: 0 | 1 }
	}>({
		url: '/add_mt/check_add_templates',
		method: 'get',
		params: data,
	}).then((res) => (res.success && res.data ? res.data : undefined))
